package com.gurun.camera.main.presenter;

import com.danale.sdk.platform.cache.DeviceCache;
import com.danale.sdk.platform.entity.device.Device;
import com.danale.sdk.throwable.PlatformApiError;
import com.danale.sdk.utils.LogUtil;
import com.gurun.camera.main.model.MainModel;
import com.gurun.camera.main.view.MainView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;


public class MainPresenterImpl implements MainPresenter {
    WeakReference<MainView> view;
    MainModel model;

    public MainPresenterImpl(final MainView view, MainModel model) {
        this.view = new WeakReference<MainView>(view);
        this.model = model;
    }


    private MainView getView() throws IllegalAccessException {
        if (view.get() == null)
            throw new IllegalAccessException("main view is recycled");
        return view.get();
    }

    /**
     * 获取设备列表 Get device list
     */

    @Override
    public void loadDevicesRemote() {
        if (this.model == null || this.view == null) {
            throw new NullPointerException("main model or view is null");
        }
        try {
            this.getView().showLoading();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        this.model.getDevices()
                .flatMap(new Func1<List<Device>, Observable<List<Device>>>() {
                    @Override
                    public Observable<List<Device>> call(List<Device> devices) {
                        List<Device> filtered = new ArrayList<Device>();
                        for (Device device : devices) {
                            if (device.getProductTypes() != null && !device.getProductTypes().isEmpty()) {
                                filtered.add(device);
                            }
                        }
                        return Observable.just(filtered);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<List<Device>>() {
                    @Override
                    public void call(List<Device> devices) {
                        try {
                            getView().hideLoading();
                            if (devices == null || devices.isEmpty()) {
                                getView().showEmptyPage();
                            } else {
                                getView().showDeviceListPage(devices);

                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        try {
                            LogUtil.e(getClass().getSimpleName(), throwable.getMessage());
                            List<Device> allDevices = DeviceCache.getInstance().getAllDevices();
                            getView().hideLoading();
                            if (allDevices == null || allDevices.isEmpty()) {
                                getView().showErrorPage();
                            } else {
                                if (throwable instanceof PlatformApiError) {
                                    getView().showError(((PlatformApiError) throwable).getErrorDescription());
                                }
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    /**
     * 注销登录 logout
     */

    @Override
    public void logout() {
//        if (this.model == null || this.view == null) {
//            throw new NullPointerException("main model or view is null");
//        }
//        try {
//            this.getView().showLoading();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }
//        NotificationManager.getInstance().cancelAllNotifications();
//        model.logout().observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Action1<LogoutResult>() {
//                    @Override
//                    public void call(LogoutResult logoutResult) {
//                        try {
//                            getView().hideLoading();
//                            if (getView() != null) {
//                                getView().hideLoading();
//                                getView().notifyLogoutState("SUCCESS");
//
//                            }
//
//                        } catch (IllegalAccessException e) {
//                            e.printStackTrace();
//                        }
//
//
//                    }
//                }, new Action1<Throwable>() {
//                    @Override
//                    public void call(Throwable throwable) {
//
//                        try {
//                            getView().hideLoading();
//                            if (getView() != null) {
//                                getView().hideLoading();
//                                getView().notifyLogoutState("SUCCESS");
//
//                            }
//
//                        } catch (IllegalAccessException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                });
    }


}
